package cn.laixueit.cart.service.impl;

import cn.laixueit.common.CodecUtils;
import cn.laixueit.common.NumberUtils;
import cn.laixueit.mapper.UserMapper;
import cn.laixueit.cart.pojo.user.User;
import cn.laixueit.cart.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Boolean check(String data, Integer type) {
        if(data==null){
            return null;
        }
        HashMap<String, Object> map = new HashMap<>();
        switch (type){
            case 1:
                map.put("username",data);
                break;
            case 2:
                map.put("phone",data);
                break;
        }
        Collection<User> collection = listByMap(map);

        return CollectionUtils.isEmpty(collection);
    }

    @Override
    public Boolean sendCode(String phone) {
        if(StringUtils.isEmpty(phone)){
            return null;
        }

        String code = NumberUtils.generateCode(6);

        HashMap<String, String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        try {
            amqpTemplate.convertAndSend("chuangmei.sms.exchange","sms.code",map);
        } catch (AmqpException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Boolean register(User user, String code) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",user.getUsername());
//        map.put("phone",user.getPhone());
        Collection<User> users = listByMap(map);
        if(!CollectionUtils.isEmpty(users)){
            return null;
        }

        String phone = user.getPhone();
        String redisCode = redisTemplate.opsForValue().get("user:smscode:" + phone);

        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        if(org.apache.commons.lang3.StringUtils.equals(code,redisCode)){
            user.setPassword(CodecUtils.md5Hex(user.getPassword(), salt));
            user.setCreated(new Date());
            save(user);
            redisTemplate.delete("user:smscode:" + phone);
            return true;
        }
        return null;
    }

    @Override
    public User queryUser(String username, String password) {
        if(org.apache.commons.lang3.StringUtils.isBlank(username)){
            return null;
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        User user = getOne(wrapper);

        String saltPwd = CodecUtils.md5Hex(password, user.getSalt());
        if(org.apache.commons.lang3.StringUtils.equals(saltPwd,user.getPassword())){
            return user;
        }
        return null;
    }
}
